import request from '@/config/axios'
import { fetchEventSource } from '@microsoft/fetch-event-source'
import { getAccessToken } from '@/utils/auth'
import { config } from '@/config/axios/config'

// 全部项目页-获取全部项目列表
export const getAllProjectsList = async () => {
  return await request.get({
    url: '/ict/projectTransfer/search/all-project'
  })
}

// 我的项目页-获取我的项目列表
export const getMylProjectsList = async () => {
  return await request.get({
    url: '/ict/projectTransfer/search/my-project'
  })
}

// 首页-批量获取项目信息
export const getProjectsByList = async (data) => {
  return await request.post({
    url: '/ict/projectTransfer/search/list',
    data
  })
}

// 获取项目信息
export const getProjectInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/project',
    params
  })
}

// 提交项目信息
export const addProjectInfo = async (data) => {
  return await request.post({
    url: '/ict/projectTransfer/add/transfer',
    data
  })
}

// 获取项目采购信息
export const getProjectPurchaseInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/all-purchase',
    params
  })
}

// 获取项目文档信息
export const getProjectDocInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/all-doc',
    params
  })
}

// 获取团队成员信息
export const getTeamMembersInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/all_member',
    params
  })
}

// 获取实施进度信息
export const getExecuteProgressInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/all-progress',
    params
  })
}

// 获取复盘信息
export const getReviewInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/all-review',
    params
  })
}

// 获取复盘信息
export const getIctSupplementInfo = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/search/ict-change',
    params
  })
}

// 文件预览和下载
export const previewAndDownloadFile = async (params) => {
  return await request.get({
    responseType: 'blob',
    url: '/kms/doc/download',
    params
  })
}

// 批量下载文件
export const batchDownloadFiles = async (data) => {
  return await request.post({
    responseType: 'blob',
    url: '/download/batch',
    data
  })
}

// 文件上传
export const uploadFile = async (data) => {
  return await request.upload({
    url: '/kms/doc/upload',
    data
  })
}

// 文件删除
export const deleteFile = async (params) => {
  return await request.get({
    url: '/ict/projectTransfer/file/delete',
    params
  })
}

// 知识库对话
export const sendChatMessageStream = async (
  data: {
    path: any
    body: any
  },
  ctrl,
  onMessage,
  onError,
  onClose
) => {
  const token = getAccessToken()
  return fetchEventSource(`${config.base_url}/kms/thirdPartyInterface/knowledge_base/completions`, {
    // return fetchEventSource(`http://172.20.10.3:7861/chat/chat/completions`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'text/event-stream',
      // 'Cache-Control': 'no-cache',
      // Connection: 'keep-alive',
      Authorization: `Bearer ${token}`
    },
    body: JSON.stringify(data),
    openWhenHidden: true,
    onmessage: onMessage,
    onerror: onError,
    onclose: onClose,
    signal: ctrl.signal
  })
}